package georah.map.tools
{

	import georah.map.MapMouseEvent;

	/**
	 *
	 * @author yuanm
	 */
	public final class MouseDefaults extends Navigation
	{
		private var _zoomThreshold:Number=2;

		override public final function get keyboardLocked():Boolean
		{
			return false;
		}

		override public final function get mouseLocked():Boolean
		{
			return true;
		}

		public final function get zoomThreshold():Number
		{
			return _zoomThreshold;
		}

		public final function set zoomThreshold(value:Number):void
		{
			_zoomThreshold=value;
		}

		override protected final function this_disableHandler(event:MapToolEvent):void
		{
			map.doubleClickEnabled=false;
			
			map.removeEventListener(MapMouseEvent.DRAG_START, map_dragStartHandler);
			map.removeEventListener(MapMouseEvent.DRAG_STOP, map_dragStopHandler);
			map.removeEventListener(MapMouseEvent.MOUSE_WHEEL, map_mouseWheelHandler);
			map.removeEventListener(MapMouseEvent.DOUBLE_CLICK, map_doubleClickHandler);

			super.this_disableHandler(event);
		}

		override protected final function this_enableHandler(event:MapToolEvent):void
		{
			super.this_enableHandler(event);

			map.addEventListener(MapMouseEvent.DRAG_START, map_dragStartHandler);
			map.addEventListener(MapMouseEvent.DRAG_STOP, map_dragStopHandler);
			map.addEventListener(MapMouseEvent.MOUSE_WHEEL, map_mouseWheelHandler);
			map.addEventListener(MapMouseEvent.DOUBLE_CLICK, map_doubleClickHandler);
			
			map.doubleClickEnabled=true;
		}

		private final function map_doubleClickHandler(event:MapMouseEvent):void
		{
			if (event.rawMouseEvent.shiftKey as Boolean)
			{
				panTo(event.where);
			}
			else if (event.rawMouseEvent.altKey as Boolean)
			{
				zoom(Navigation.ZOOM_OUT, event.where);
			}
			else
			{
				zoom(Navigation.ZOOM_IN, event.where);
			}
		}

		private final function map_dragStartHandler(event:MapMouseEvent):void
		{
			dragPanStart(event.where);
		}

		private final function map_dragStopHandler(event:MapMouseEvent):void
		{
			dragPanStop(event.where);
		}

		private final function map_mouseWheelHandler(event:MapMouseEvent):void
		{
			if (event.rawMouseEvent.delta > zoomThreshold)
			{
				zoom(Navigation.ZOOM_IN);
			}
			else if (event.rawMouseEvent.delta < -zoomThreshold)
			{
				zoom(Navigation.ZOOM_OUT);
			}
		}

	}
}
